<?php


namespace App\Services\Business\Models;

use App\Services\Business\Models\Base;
use App\Services\Member\Models\User;
use App\Services\Member\Models\Activity;
use Carbon\Carbon;


class Conquer extends Base
{


    protected $table = 'conquer';
    public const STATUS_TO = -1;
    public const STATUS_STARTED = 1;
    public const STATUS_CLOSED = 2;


    public const STATUS_TEXT = [
        self::STATUS_TO => '未开始',
        self::STATUS_STARTED => '进行中',
        self::STATUS_CLOSED => '已结束',
    ];
    protected $appends = [
        'status_text','has_started'
    ];


    protected $fillable = [
        'user_id' ,'title','weight','thumb','is_public','is_hide','start_at','days','qr_group','user_count'
    ];

    public function getHasStartedAttribute()
    {
        $currentTime = Carbon::now();
        return $currentTime->greaterThan($this->start_at);
    }

    public function getStatusTextAttribute()
    {
        return $this->statusText();
    }

    public function statusText(): string
    {
        return self::STATUS_TEXT[$this->status] ?? '';
    }



    public function user()
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    public function conquer()
    {
        return $this->belongsTo(Conquer::class, 'user_id');
    }

    public  function  users()
    {
        return $this->belongsToMany(User::class, 'user_conquer', 'conquer_id', 'user_id')->withTimestamps();
    }


    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function fragments()
    {
        return $this->hasMany(WeightFragment::class, 'conquer_id');
    }
    public  function  activity()
    {
        return $this->morphMany(Activity::class, 'activityable');
    }








}
